package com.yjt.javatest.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yjt.javatest.entity.StudentTest;
import com.yjt.javatest.entity.StudentTestDao;
import com.yjt.javatest.entity.vo.StudentTestInfo;
import com.yjt.javatest.entity.vo.StudentTestVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface StudentTestMapper extends BaseMapper<StudentTest> {
    @Select("select s.id, s.test_id, s.title, " +
            "case when t.answer_release_at < NOW() then s.score else -1 end as score from student_test s left join test t on " +
            "s.test_id = t.test_id and s.user_id = t.user_id and s.user_id = #{userId}")
    List<StudentTestInfo> getStudentTestInfoByUserId(Long userId);
    @Insert("insert into student_test " +
            "(user_id, test_id, title, score, questions, correct_answers) values " +
            "(#{info.userId}, #{info.testId}, #{info.title}, #{info.score}, #{questions}, #{correctAnswers})")
    int insert(StudentTest info, String questions, String correctAnswers);

    @Update("update student_test set score = #{score} where user_id = #{userId} and test_id = #{testId}" )
    int updateScore(Long userId, Long testId, Integer score);

    @Update("update student_test set  user_answers = #{userAnswers} where user_id = #{userId} and test_id = #{testId}" )
    int updateUserAnswers(Long userId, Long testId, String userAnswers);

    @Select("<script>" +
            "select s.user_id, s.test_id, s.score,u.name from student_test s " +
            "left join user u on u.user_id = s.user_id" +
            "where test_id in" +
            "<foreach collection = 'testIds' item = 'item' open='(' separator = ',' close = ')'>" +
            "#{item}" +
            "</foreach>" +
            "</script>" )
    List<StudentTestVo> getStudentTestInfoByTestIds(List<Integer> testIds);

    @Select("select * from student_test where test_id = #{testId} and user_id = #{userId}")
    StudentTestDao getByTestIdAndUserId(Long testId, Long userId);
}
